home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / startup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-17  |  9.4 KB  |  370 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: startup.c,v 1.1 89/03/17 08:21:21 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/startup.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/startup.c,v $$Revision: 1.1 $";
  12.  
  13. /* read and process startup file */ /* this needs to be redone */
  14.  
  15. #include <stdio.h>
  16. #include "bitmap.h"
  17. #include "defs.h"
  18. #include "font.h"
  19.  
  20. #define MAXLINE        128
  21. #define MAXFIELDS    100
  22.  
  23. struct font *Get_font();
  24.  
  25. static int newx = -1, newy = -1, newdx, newdy, newfont, newflags;
  26. static char newinit[MAXLINE];
  27. static char *newshell[MAXFIELDS];
  28. static char newstart[MAXLINE];
  29. static char *initcmd, *suspendcmd, *resumecmd, *quitcmd;
  30.  
  31. startup(name)        /* do startup file */
  32. char *name;        /* name of startup file */
  33.    {
  34.    FILE *file = fopen(name,"r");        /* file to read commands from */
  35.    int x=0 ,y=0 ,dx=0 ,dy=0 ,fnt=0 ;        /* window coords, font # */
  36.    char *trans(), *save_line(), *strcat();
  37.    char line[MAXLINE];                /* space to store stuff */
  38.    char start[MAXLINE];                /*  "  */
  39.    char init[MAXLINE];                /*  "  */
  40.    char *shell[MAXFIELDS];            /*  "  */
  41.    char *fields[MAXFIELDS];            /*  "  */
  42.    int curr_font = 0;                /* font # for this window */
  43.    int flags = 0;                /* window flags */
  44.    register int i;
  45.    int count,  gotwindow = 0, gotnewwindow = 0;            
  46.    int setactive = 0, activesetid = 0;
  47.    int windowsetid = 0;
  48.  
  49.    bzero(start,MAXLINE);
  50.    bzero(line,MAXLINE);
  51.    bzero(init,MAXLINE);
  52.    bzero(shell,MAXFIELDS);
  53.    bzero(fields,MAXFIELDS);
  54.  
  55.    if (file == NULL) 
  56.       return(-1);
  57.  
  58.  
  59.    /* process each line in startup file */
  60.  
  61.    while (fgets(line,sizeof(line),file) != NULL) {
  62. #ifdef DEBUG
  63.       dprintf(S)(stderr,"*** got: %s \r\n",line);
  64. #endif
  65.       count = parse(line,fields);
  66.  
  67.       if (*fields == (char *) 0)
  68.          continue;
  69.  
  70.       else if (strcmp(*fields,"window")==0 || strcmp(*fields,"done")==0) {
  71.  
  72.          if (gotnewwindow==1) {
  73.         gotnewwindow = 0;
  74.         newx = x;
  75.         newy = y;
  76.         newdx = dx;
  77.         newdy = dy;
  78.         newfont = curr_font;
  79.         strcpy(newstart, start);
  80.         strcpy(newinit, init);
  81.         for( i = 0; shell[i]; i++ ) {
  82.            if( newshell[i] )
  83.           free( newshell[i] );
  84.            newshell[i] = shell[i];
  85.            shell[i] = (char*)0;
  86.            }
  87.             gotwindow = 0;
  88.         newshell[i] = (char*)0;
  89.             gotwindow = 0;
  90.             bzero(start,MAXLINE);
  91.             bzero(init,MAXLINE);
  92.             curr_font = 0;
  93.             flags = 0;
  94.         windowsetid = 0;
  95.         }
  96.       /* got a window */
  97.  
  98.          if (gotwindow==1) {
  99.             if (check_window(x,y,dx,dy,curr_font) == 0) {
  100.                gotwindow = 0;
  101.                bzero(start,MAXLINE);
  102.                bzero(init,MAXLINE);
  103.                curr_font = 0;
  104.                flags = 0;
  105.            windowsetid = 0;
  106.                continue;
  107.                }
  108.  
  109.         if( !active ) {
  110.            MOUSE_OFF(mousex,mousey);
  111.                erase_win(screen,0,0);
  112.            MOUSE_ON(mousex,mousey);
  113.         }
  114.             dowindow( x, y, dx, dy, curr_font, shell, flags, init, start );
  115.  
  116.         if( windowsetid ) {
  117.            ACTIVE(setid) = windowsetid;
  118.            windowsetid = 0;
  119.         }
  120.  
  121.         if( setactive  &&  activesetid == 0 )
  122.            activesetid = ACTIVE(setid);
  123.    
  124.             if( setactive )
  125.            topwin( activesetid, -1 );
  126.         if( *shell )
  127.            free( *shell );
  128.             *shell = '\0';
  129.             flags = 0;
  130.             bzero(start,MAXLINE);
  131.             bzero(init,MAXLINE);
  132.             curr_font = 0;
  133.             }
  134.  
  135.          if (strcmp(*fields,"window")==0 && count >=5) {
  136.  
  137.             gotwindow = 1;
  138.             x = atoi(fields[1]);
  139.             y = atoi(fields[2]);
  140.             dx = atoi(fields[3]);
  141.             dy = atoi(fields[4]);
  142.             if (count > 5 && (fnt=atoi(fields[5]))<=MAXFONT) {
  143.            char lastchar();
  144.            struct font *fp = Get_font( fnt );
  145.  
  146.            if( lastchar( fields[3] ) == 'c' )
  147.           dx = dx*fp->head.wide + 2*SUM_BDR;
  148.            if( lastchar( fields[4] ) == 'c' )
  149.           dy = dy*fp->head.high + 2*SUM_BDR;
  150.                curr_font = fnt;
  151.            }
  152.             }
  153.          else
  154.             gotwindow = 0;
  155.          }
  156.  
  157.       else if (strcmp(*fields,"initcmd")==0 && count >2) {
  158.         save_fields( &initcmd, fields );
  159.         do_cmd( 'i' );
  160.      }
  161.       else if (strcmp(*fields,"suspendcmd")==0 && count >2) {
  162.         save_fields( &suspendcmd, fields );
  163.      }
  164.       else if (strcmp(*fields,"resumecmd")==0 && count >2) {
  165.         save_fields( &resumecmd, fields );
  166.      }
  167.       else if (strcmp(*fields,"quitcmd")==0 && count >2) {
  168.         save_fields( &quitcmd, fields );
  169.      }
  170.       else if (strcmp(*fields,"font")==0 && count >2) {
  171.          fnt = atoi(fields[1]);
  172. #ifdef DEBUG
  173.          dprintf(S)(stderr,"got font %s (%d)\r\n",fields[2],fnt);
  174. #endif
  175.          if (fnt > 0  &&  fnt <= MAXFONT) {
  176.             if (fontlist[fnt-1])
  177.                free(fontlist[fnt-1]);
  178.             fontlist[fnt-1] = save_line(fields[2]);
  179.             }
  180.          }
  181.  
  182.       else if (strcmp(*fields,"map")==0 && count >8) {
  183.          for(i=0;i<8;i++)
  184.             map_mouse(i,atoi(fields[i+1]));
  185. #ifdef DEBUG
  186.          dprintf(S)(stderr,"got mouse map \r\n");
  187. #endif
  188.          }
  189.  
  190.       else if (strcmp(*fields,"slide")==0 && count > 2) {
  191.          set_slide(atoi(fields[1]),atoi(fields[2]));
  192. #ifdef DEBUG
  193.          dprintf(S)(stderr,"set menu slide %d,%d\r\n",
  194.                     atoi(fields[1]),atoi(fields[2]));
  195. #endif
  196.          }
  197.  
  198.       else if (strcmp(*fields,"page")==0 && count > 2) {
  199.          set_page(atoi(fields[1]),atoi(fields[2]));
  200. #ifdef DEBUG
  201.          dprintf(S)(stderr,"set menu page %d,%d\r\n",
  202.                     atoi(fields[1]),atoi(fields[2]));
  203. #endif
  204.          }
  205.  
  206.       else if (strcmp(*fields,"active")==0 && gotwindow==1) {
  207.      setactive = 1;
  208.      activesetid = 0;
  209.       }
  210.       else if (strcmp(*fields,"windowsetid")==0 && count>1 && gotwindow==1) {
  211.      windowsetid = atoi( fields[1] );
  212.       }
  213.       else if (strcmp(*fields,"newwindow")==0 && gotwindow==1) {
  214.      gotnewwindow = 1;
  215.       }
  216.       else if (strcmp(*fields,"shell")==0 && count>1 && gotwindow==1) {
  217.          for(i=0;i<count-1;i++) {
  218.             if (shell[i])
  219.                free(shell[i]);
  220.             shell[i] = save_line(fields[i+1]);
  221.             }
  222.          if (shell[count-1]) {
  223.             free (shell[count-1]);
  224.             shell[count-1] = (char *) 0;
  225.             }
  226.          }
  227.       else if (strcmp(*fields,"init")==0) {
  228.          for(i=1;fields[i]!=(char *)0;i++) {
  229.         if( i > 1 )
  230.            strcat(init," ");
  231.             strcat(init,fields[i]);
  232.         }
  233.          trans(init);
  234.          }
  235.  
  236.       else if (strcmp(*fields,"start")==0) {
  237.          for(i=1;fields[i]!=(char *)0;i++) {
  238.         if( i > 1 )
  239.            strcat(start," ");
  240.             strcat(start,fields[i]);
  241.         }
  242.          trans(start);
  243.          }
  244.  
  245.       else if (strcmp(*fields,"flags")==0 && count > 1 && gotwindow==1) {
  246.          for(i=1;i<count;i++) {
  247.             if (strcmp(fields[i],"expose")==0)
  248.                flags |= W_EXPOSE;
  249.             else if (strcmp(fields[i],"background")==0)
  250.                flags |= W_BACKGROUND;
  251.             else if (strcmp(fields[i],"nokill")==0)
  252.                flags |= W_NOKILL;
  253. #ifdef DEBUG
  254.             dprintf(S)(stderr,"Flags: %d (%s)\r\n",flags,fields[i]);
  255. #endif
  256.             }
  257.          }
  258.  
  259.       else {
  260. #ifdef DEBUG
  261.          dprintf(S)(stderr,"invalid line: %s\r\n",line);
  262. #endif
  263.          }
  264.       }
  265.    fclose(file);
  266.    return(0);
  267.    }
  268.  
  269.  
  270. /*    Given a character pointer and a pointer to an array of strings,
  271.     catenate the strings into fresh memory and put the address of the copy
  272.     into the character pointer.
  273.     free() any space originally pointed to by the character pointer.
  274. */
  275. save_fields( cpp, fields )
  276. char    **cpp;
  277. char    *fields[];
  278. {
  279.     register int    i;
  280.     char        savestr[MAXLINE];
  281.  
  282.     if( *cpp )
  283.         free( *cpp );
  284.     *savestr = '\0';
  285.     for( i=1;  fields[i]!=(char *)0;  i++ ) {
  286.         if( i > 1 )
  287.             strcat(savestr, " ");
  288.         strcat(savestr, fields[i]);
  289.     }
  290.     trans(savestr);
  291.     *cpp = save_line( savestr );
  292. }
  293.  
  294.  
  295. do_cmd( flag )
  296. char    flag;
  297. {
  298.     switch( flag ) {
  299.     case 'i':
  300.         system( initcmd );
  301.         break;
  302.     case 's':
  303.         system( suspendcmd );
  304.         break;
  305.     case 'r':
  306.         system( resumecmd );
  307.         break;
  308.     case 'q':
  309.         system( quitcmd );
  310.         break;
  311.     }
  312. }
  313.  
  314.  
  315. initwindow()
  316. {
  317.     dowindow( newx, newy, newdx, newdy, newfont, newshell, newflags,
  318.     newinit, newstart );
  319. }
  320.  
  321.  
  322. static
  323. char
  324. lastchar( cp )
  325. char *cp;
  326. {
  327.    int length = strlen(cp);
  328.    if( !length )
  329.       return '\0';
  330.    return  cp[ length-1 ];
  331. }
  332.  
  333.  
  334. static
  335. dowindow( x, y, dx, dy, font, shell, flags, init, start )
  336. int    x, y, dx, dy, font;
  337. char    *shell[];
  338. int    flags;
  339. char    *init, *start;
  340. {
  341.     struct font *fp = Get_font( font );
  342.     int i;
  343.  
  344. #ifdef DEBUG
  345.     dprintf(S)(stderr,"starting shell %s\r\n",shell ? *shell : "???");
  346. #endif
  347.     if( x < 0 )
  348.        x = 32 + 16*next_windowset_id();
  349.     if( y < 0 )
  350.        y = 32 + 16*next_windowset_id();
  351.     if( dx <= 0 )
  352.       dx = 80*fp->head.wide + 2*SUM_BDR;
  353.     if( dy <= 0 )
  354.       dy = 24*fp->head.high + 2*SUM_BDR;
  355.     MOUSE_OFF(mousex,mousey);
  356.     create_window(x, y, dx, dy, font, *shell?shell:0);
  357.     MOUSE_ON(mousex,mousey);
  358.  
  359.     if (flags)
  360.        ACTIVE(flags) |= flags;
  361.     if (*init)
  362.        put_window(active,init,strlen(init));
  363.     if (*start)
  364.        i = Write(ACTIVE(to_fd),start,strlen(start));
  365. #ifdef DEBUG
  366.     dprintf(S)(stderr,"%s: start string %d/%d %s\r\n",ACTIVE(tty),
  367.            i,strlen(start),start);
  368. #endif
  369. }
  370.